package com.tchintech.pay.model.base;

import com.google.gson.Gson;

import java.net.URLDecoder;
import java.net.URLEncoder;

import com.tchintech.pay.listeners.OnNetRequestListener;
import com.tchintech.pay.model.entity.Memberinfo;
import com.tchintech.pay.model.entity.ResultBean;
import com.tchintech.pay.server.RetrofitService;
import com.tchintech.pay.utils.StringUtil;
import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action0;
import rx.schedulers.Schedulers;

/**
 * cn.woogo.micp.model.base
 * 数据同步
 *
 * @auth John
 * Created  on 2016/5/17 0017.
 */
public class BaseDataImpl implements BaseData {
    @Override
    public void queryData(String path, Object o, Memberinfo memberinfo, final OnNetRequestListener listener) {

        String json = StringUtil.object2json(o);
        String memberinfoJson = StringUtil.object2json(memberinfo);
        try {
            json = URLEncoder.encode(json, "UTF-8");
            memberinfoJson = URLEncoder.encode(memberinfoJson, "UTF-8");
            RetrofitService retrofitService = new RetrofitService().getInstance();

            Observable<String> observable = retrofitService.createQueryApi().queryBaseData(path, json, memberinfoJson);
            observable.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())//由于是数据同步，所有操作都在io线程里操作
                    .doOnSubscribe(new Action0() {
                        @Override
                        public void call() {
                            listener.onStart();
                        }
                    })
                    .subscribe(new Subscriber<String>() {
                        @Override
                        public void onCompleted() {
                            //仅成功后会回调
                            listener.onFinish();
                        }

                        @Override
                        public void onError(Throwable e) {
                            e.printStackTrace();
                            listener.onFailure(e);
                            listener.onFinish();
                        }

                        /**
                         * Provides the Observer with a new item to observe.
                         * <p/>
                         * The {@link Observable} may call this method 0 or more times.
                         * <p/>
                         * The {@code Observable} will not call this method again after it calls either {@link #onCompleted} or
                         * {@link #onError}.
                         *
                         * @param s the item emitted by the Observable
                         */
                        @Override
                        public void onNext(String s) {
                            //  System.out.println(s);
                            try {
                                String reString = URLDecoder.decode(s, "UTF-8");
                                Gson gson = new Gson();
                                ResultBean rb = gson.fromJson(reString, ResultBean.class);
                                // System.out.println(rb);
                                listener.onSuccess (rb);
                            } catch (Exception e)  {
                                e.printStackTrace();
                            }
                        }

                    });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
